<?php

/**
 * 数据库常规动作类
 * 
 * @package    01CMS
 * @subpackage system
 * @author     rolong at vip.qq.com
 * @version    1.0.0
 * @link       http://www.01cms.com
 */

class Action
{
    public $goUrl;
    
    private $data;
    private $id;
    private $Base;
    private $stayTime;
    private $templateSuffix;
    private $Db;
    private $js;

    function __construct ()
    {
        $this->Base = & Base::i();
        $this->Db = isset($this->Base->Db) && is_object($this->Base->Db) ? $this->Base->Db : $this->Base->Load->model('Db');
        $this->templateSuffix = empty($this->Base->templateSuffix) ? '' : $this->Base->templateSuffix;
        $this->id = empty($this->Base->id) ? 0 : ereg_replace('[^0-9,]', '', $this->Base->id);
        $this->data = empty($this->Base->data) ? array() : $this->Base->data;
        $this->js = empty($this->Base->js) ? 0 : $this->Base->js;
    }

    function lists ()
    {
        if (empty($this->Base->lists))
        {
            $this->Base->lists = $this->Db->lists();
            if (isset($this->Base->dealListsModelMethod))
            {
                $method = $this->Base->dealListsModelMethod;
                $this->Base->lists = $this->Db->{$method}($this->Base->lists);
            }else if (isset($this->Base->dealListsControllerMethod))
            {
                $method = $this->Base->dealListsControllerMethod;
                $this->Base->lists = $this->Base->{$method}($this->Base->lists);
            }
        }
        
        $this->Base->Load->view('smarty', $this->templateSuffix);
    }

    function add ()
    {
        $this->Base->Load->view('smarty', 'add' . $this->templateSuffix);
    }

    function insert ()
    {
        $status = $this->Db->insert($this->data, $this->Db->table);
        
        if ($status)
        {
            response('操作成功', TRUE, $this->js);
        }
        else
        {
            response('操作失败', FALSE, $this->js);
        }
    }

    function mod ()
    {
        $this->Base->data = $this->Db->getRow("SELECT * FROM `{$this->Db->table}` WHERE `id` = '{$this->id}'");
        $this->Base->Load->view('smarty', 'mod' . $this->templateSuffix);
    }

    function update ()
    {
        $status = $this->Db->update($this->data, $this->id);
        
        if ($status)
        {
            response('操作成功', TRUE, $this->js);
        }
        else
        {
            response('操作失败', FALSE, $this->js);
        }
    }

    function del ()
    {
        if (strpos($this->id, ',') !== FALSE)
        {
            $ids = explode(',', $this->id);
            
            $status = TRUE;
            
            foreach ($ids as $id)
            {
                $sta = $this->Db->delete($id);
                
                if (! $sta)
                {
                    $status = FALSE;
                }
            }
        
        }
        else
        {
            $status = $this->Db->delete($this->id);
        }
        
        if ($status)
        {
            response('删除成功', TRUE, $this->js);
        }
        else
        {
            response('操作失败', FALSE, $this->js);
        }
    }

    function ordering ()
    {
        $ids = explode(',', $this->id);
        
        $i = 0;
        
        $status = TRUE;
        
        foreach ($ids as $id)
        {
            $status = $this->Db->update(array('ordering' => $i), "id = {$id}");
            
            if (! $status)
            {
                $status = FALSE;
            }
            
            $i ++;
        }
        
        if ($status)
        {
            response('成功更新排序', TRUE, $this->js);
        }
        else
        {
            response('排序更新失败', FALSE, $this->js);
        }
    }
}

/* End of file /01CMS/01mvc/lib/Action.php */